//
//  HTTPRequest.swift
//  DYSwiftFramework
//
//  Created by 黄德玉 on 2018/8/6.
//  Copyright © 2018年 none. All rights reserved.
//

import Alamofire

enum HTTPError : Error{
    case serverError(code:String,description:String)
    case dataError(code:String,description:String)
}

class HTTPRequest{
    public var router:String?           //url后面的路由路径
    public var parameters:Any?          //请求参数
    public var baseURL:URLConvertible?
    public var needEncryp:Bool?         // 还没实现
    public var needIndependentSession:Bool? //是否需要独立的session，这个需要注意，需要在请求结束后调用释放session方法
    public var method:HTTPMethod = .post
    public var cachePolicy: NSURLRequest.CachePolicy?
    public var timeout:TimeInterval?
    public var requestHeader:[String:String]? = ["ContentType":"application/x-www-form-urlencoded;charset=utf-8"]
    public var bodyEncodeBlock:(()->Void)?   // 这是对请求参数进行额外的包装
    public var responseCheckBlock:((AFError?,DataResponse<Any>?)->(Bool,HTTPError?))?       // 这是对响应对象进行格式合理化检查，
    public var reponseDecodeBlock:((DataResponse<Any>)->Any?)?                              // 这是对响应数据模型进行拆解所需要的最终样式
}

extension HTTPRequest{
    public static func defaultRequest(_ parameters:Any?,_ router:String) -> HTTPRequest{
        let request = HTTPRequest();
        request.baseURL = "http://www.baidu.com";
        request.bodyEncodeBlock = { [weak request] in
            guard request != nil else { return }
//            strongRequest.parameters
        }
        request.responseCheckBlock = {[weak request] (error,response) in
            return (true,nil)
        }
        request.reponseDecodeBlock = {[weak request] (response) in
            return nil;
        }
        return request;
    }
}

